package usi.ch.onto.main;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import usi.ch.onto.lucene.indexing.Indexer;
import usi.ch.onto.lucene.scoring.IndexTermItem;
import usi.ch.onto.lucene.scoring.KeyTermExtractor;
import usi.ch.onto.yago.ExportResults;

/**
 * Tester for indexing the files with Lucene
 * 
 * @param FILES_TO_INDEX
 *            - main directory containing sub - directories divided by user
 *            interest.
 * @param INDEX_LOCATION
 *            - main container for the created indexes
 * 
 * 
 * **/

public class MainIndexer {
	private static String RESOURCES_PATH = "../yagoaida_resources/";

	private static String FILES_TO_INDEX = RESOURCES_PATH + "filestobigindex"; 
	private static String INDEXES_LOCATION = RESOURCES_PATH + "index";
	private static String COL_INDEXES_LOCATION = FILES_TO_INDEX + "-indexcol";
	
	private static String COMPLETE_FILE_PATH = RESOURCES_PATH + "complete.csv";
	private static final String FILES_TO_STRIP = RESOURCES_PATH
			+ "filestostrip";

	public static void main(String[] args) throws Exception {
		//testIndexer(FILES_TO_INDEX, INDEXES_LOCATION);
		
		//List<String> listTerms = getIndexTerms();
//		System.out.println("start term dumping");
//		for(String term : listTerms){
//			System.out.println(term);
//		}
		System.out.print("get TOP term index list and output file...");
		Collection<IndexTermItem> listTerms = getIndexTopTermList(70.0F);
		ExportResults.outputTermItemsList(listTerms , COMPLETE_FILE_PATH);
		System.out.println("done");
		
//		System.out.print("get index list and output file...");
//		List<IndexTermItem> listCompleteTerms = getIndexCompleteList();
//		ExportResults.outputTermItemsList(listCompleteTerms , COMPLETE_FILE_PATH);
//		System.out.println("done");

	}


	
	// Loop through FILES_TO_INDEX directory and creates spearate index for each
	// of sub - directories by calling Indexer
	public static void testIndexer(String filestoindex, String indexlocation)
			throws Exception {
		Indexer m = new Indexer(indexlocation, filestoindex);
		m.makeIndex();

	}


	public static Collection<IndexTermItem> getIndexTopTermList(float topPercentage) throws IOException {
		KeyTermExtractor itReader = new KeyTermExtractor(true);
		itReader.retrieveTerms(new File(INDEXES_LOCATION));
		return itReader.getTopTermList(topPercentage);
	}	
	
	
	public static List<IndexTermItem> getIndexCompleteList() throws IOException {
		KeyTermExtractor itReader = new KeyTermExtractor(true);
		itReader.retrieveTerms(new File(INDEXES_LOCATION));
		List<IndexTermItem> termlist = itReader.getCompleteList();
		return termlist;
	}

	/**
	 *Reads the term list from the index
	 * 
	 * @return termlist - list with frequent terms in index
	 */
	public static List<String> getIndexTerms() throws IOException {
		KeyTermExtractor itReader = new KeyTermExtractor();

		itReader.retrieveTerms(new File(INDEXES_LOCATION));
		ArrayList<String> termlist = itReader.getTermList();
		return termlist;
	}
}
